//
//  LCSendSugarDemo6.m
//  WHC_AutoLayoutExample
//
//  Created by 吴海超 on 16/3/1.
//  Copyright © 2016年 吴海超. All rights reserved.
//

#import "LCSendSugarDemo6.h"

@interface LCSendSugarDemo6 ()

@property (strong, nonatomic) UILabel *titleUILabel;
@property (strong, nonatomic) NSMutableArray <UIButton *> *numButtonArr;
@property (strong, nonatomic) UITextField *textField;//自定义金额
@property (strong, nonatomic) LCMessageTextField *messageTextField;
@property (strong, nonatomic) UILabel *messageTitleUILabel;
@property (assign, nonatomic) NSInteger didSelected;
@property (strong, nonatomic) NSArray *someInts;
@end

@implementation LCSendSugarDemo6

- (void)dealloc
{
    [self endEditing:true];
    [[NSNotificationCenter defaultCenter] removeObserver:self];
}
- (instancetype)init
{
    self = [super init];
    if (self) {
        [self makeView];
    }
    return self;
}
- (void) makeView {
    self.backgroundColor = RGBColor(235, 235, 235);
    [self setRadius:7];
//    self.navigationItem.title = @"送福音值";
//    self.edgesForExtendedLayout = UIRectEdgeNone;
//    self.tabBarController.tabBar.translucent = NO;
    
    self.titleUILabel = [UILabel new];
    [self addSubview:self.titleUILabel];
    self.titleUILabel.text = @"送福音值";
    self.titleUILabel.font = [UIFont systemFontOfSize:21];
    [self.titleUILabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self).offset(30);
        make.left.equalTo(self).offset(30);
    }];
    
    UIButton *titleBut = [UIButton new];
    [titleBut setBackgroundImage:[[UIImage imageNamed:@"我的_福音值"] imageWithTintColor:redUIColor] forState:(UIControlStateNormal)];
    [self addSubview:titleBut];

    [titleBut mas_makeConstraints:^(MASConstraintMaker *make) {
//        make.top.equalTo(self).offset(30);
        make.left.equalTo(self.titleUILabel.mas_right).offset(6);
        make.width.mas_equalTo(25);
        make.height.mas_equalTo(25);
        make.centerY.equalTo(titleBut.mas_centerY).offset(0);
        make.top.equalTo(self.titleUILabel);
        make.bottom.equalTo(self.titleUILabel);

    }];
    
    UIButton *sugarListBut = [UIButton new];
    self.sugarListBut = sugarListBut;
    [sugarListBut setTitle:@"赠送列表" forState:(UIControlStateNormal)];
    sugarListBut.titleLabel.font = [UIFont systemFontOfSize:21];
    [sugarListBut setTitleColor:[UIColor colorWithWhite:0.5 alpha:0.9] forState:UIControlStateNormal];
    [self addSubview:sugarListBut];
    
    [sugarListBut mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.equalTo(titleBut.mas_centerY).offset(0);
        make.right.equalTo(self).offset(-30);
        make.top.equalTo(self.titleUILabel);
        make.bottom.equalTo(self.titleUILabel);
    }];
    //
    
    /// 样式3
    WHC_StackView * stackView2 = WHC_StackView.new;
    [self addSubview:stackView2];
    stackView2.whc_LeftSpace(30)
    .whc_RightSpace(30)
    .whc_TopSpaceToView(20,self.titleUILabel)
    .whc_Height(120);
    self.someInts = @[@"5", @"20", @"50", @"100", @"500", @"自定义"];
    for(NSInteger i = 0; i < self.someInts.count; i++) {
        if (i == self.someInts.count - 1) {
            UITextField *textField = [self makeTextField:self.someInts[i]];
            textField.tag = i+10;
            [stackView2 addSubview:textField];
        }else{
            UIButton *button = [self makeBut:self.someInts[i]];
            button.tag = i+10;
            [stackView2 addSubview:button];
        }
    }
    stackView2.whc_Column = 3;
    stackView2.whc_Orientation = All;
    stackView2.whc_SegmentLineSize = 11;
    [stackView2 whc_StartLayout];

    [self setBorder: stackView2];
    
    LCMessageTextField *messageTextField = [[LCMessageTextField alloc] init];
    messageTextField.font = [UIFont systemFontOfSize:20];
    messageTextField.backgroundColor = [UIColor whiteColor];
    messageTextField.borderStyle = UITextBorderStyleNone;
    messageTextField.layer.borderColor = RGBColor(255, 255, 255).CGColor;
    messageTextField.layer.borderWidth = 1;
    messageTextField.layer.cornerRadius = 5;
    messageTextField.placeholder = @"写一段鼓励的话";
    _messageTextField = messageTextField;
    [self insertSubview:messageTextField atIndex:0];
    
    [messageTextField mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(stackView2.mas_bottom).offset(20);
        make.left.equalTo(stackView2.mas_left).offset(0);
        make.right.equalTo(stackView2.mas_right).offset(0);
        make.height.offset(58);
    }];
    UILabel *messageTitleUILabel = [[UILabel alloc] init];
    self.messageTitleUILabel = messageTitleUILabel;
    [self.messageTextField addSubview:messageTitleUILabel];
    messageTitleUILabel.text = @"留言:";
    messageTitleUILabel.textColor = titleColorLSZ;
    messageTitleUILabel.font = [UIFont systemFontOfSize:19];

    [messageTitleUILabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.equalTo(messageTextField.mas_centerY);
        make.left.offset(20);
    }];
    UIButton *undoButton = [[UIButton alloc] init];
    self.undoButton = undoButton;
    [self addSubview:undoButton];
    [undoButton setTitleColor:redUIColor forState:UIControlStateNormal];
//    undoButton.backgroundColor = [UIColor whiteColor];
    [undoButton setTitle:@"取消" forState:(UIControlStateNormal)];
    CGFloat space = 20;
    if (ScreenWidth == 320) {
        space = 10;
    }
    [undoButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(messageTextField.mas_bottom).offset(40);
        make.left.equalTo(stackView2.mas_left).offset(space);
        make.width.mas_equalTo(100);
    }];
    
    UIButton *submitButton = [[UIButton alloc] init];
    self.submitButton = submitButton;
    [self addSubview:submitButton];
//    submitButton.backgroundColor = [UIColor whiteColor];
    [submitButton setTitleColor:redUIColor forState:UIControlStateNormal];
    [submitButton setTitle:@"确认" forState:(UIControlStateNormal)];
    [submitButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(messageTextField.mas_bottom).offset(40);
        make.width.mas_equalTo(100);
        make.right.equalTo(stackView2.mas_right).offset(-space);
    }];
    [submitButton addTarget:self action:@selector(submitButtonClick:) forControlEvents:(UIControlEventTouchUpInside)];

}
- (void)submitButtonClick:(UIButton *)send {
    if([[_UserM usercode] isEqualToString:@""]){
        pushAccLoginVCNotification;
        return;
    }

    NSString *credit = @"0";
    if (self.didSelected-10 == 5) {
        credit = self.textField.text;
        if (credit.length == 0) {
            [SVProgressHUD showErrorWithStatus:@"请输入赠送的福音值数量!"];
        }
    }else if (self.didSelected-10 < 5 && self.didSelected-10 >= 0) {
        credit = self.someInts[self.didSelected-10];
    }else{
        [SVProgressHUD showErrorWithStatus:@"请选择赠送的福音值数量!"];
    }
    NSArray *array = @[credit, self.messageTextField.text, self.mg.id_id];
    @weakify(self)
    [[LCWebServicesManager sharedInstance] addDonateWithArray:array type:1 success:^(NSDictionary *dict) {
        @strongify(self)
        NSLog(@"dict = %@", dict);
        NetWorkModel *netWorkModel = [NetWorkModel yy_modelWithJSON:dict];
        if (netWorkModel.status == 1) {
            [SVProgressHUD showSuccessWithStatus:netWorkModel.msg];
            //点击方
            if (self.successBlock != nil) {
                self.successBlock();
            }
        }
    } failure:^(NSError *error) {
        NSLog(@"error = %@", error);
    }];
}

- (UITextField *)makeTextField:(NSString *) title {
    UITextField *textField = UITextField.new;
    _textField = textField;
    textField.placeholder = title;
    textField.backgroundColor = [UIColor whiteColor];
    [textField border:[UIColor colorWithWhite:0.8 alpha:0.8] width:1 CornerRadius:5];
    textField.font = [UIFont systemFontOfSize:20];
    textField.textColor = [UIColor colorWithWhite:0.7 alpha:0.9];
    textField.textAlignment = NSTextAlignmentCenter;
    [self textFiledEditChangedWithTextField:textField];
    __weak typeof(self) weakSelf = self;
    [textField tapGesture:^(UIGestureRecognizer *ges) {
        weakSelf.didSelected = textField.tag;
        [weakSelf changeDark];
        [textField border:redUIColor width:1 CornerRadius:5];
        textField.textColor = redUIColor;
        [textField becomeFirstResponder];
        textField.placeholder = @"";
    }];
    return textField;
}
- (void)textFiledEditChangedWithTextField:(UITextField *)textField{
    //检测文本改变
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(putInTextFieldEditChanged:) name:UITextViewTextDidChangeNotification object:textField];
}
- (void)putInTextFieldEditChanged:(NSNotification *)obj {
    UITextField *textField = (UITextField *)obj.object;
    NSString *string = textField.text;
    NSLog(@"string = %@", string);
    [self changeDark];
}
- (void)changeDark {
    [self endEditing:true];
    _textField.placeholder = @"自定义";
    [_textField border:[UIColor colorWithWhite:0.8 alpha:0.8] width:1 CornerRadius:5];
    _textField.textColor = [UIColor colorWithWhite:0.7 alpha:0.9];
    for (NSInteger i = 0; i < self.numButtonArr.count; i++) {
        self.numButtonArr[i].selected = false;
        [self.numButtonArr[i] border:[UIColor colorWithWhite:0.8 alpha:0.8] width:1 CornerRadius:5];
    }
}
- (UIButton *)makeBut:(NSString *) title {
    UIButton * but = UIButton.new;
    but.backgroundColor = [UIColor whiteColor];
    [but border:[UIColor colorWithWhite:0.8 alpha:0.8] width:1 CornerRadius:5];
    [but setTitle:title forState:(UIControlStateNormal)];
    but.titleLabel.font = [UIFont systemFontOfSize:24];
    [but setTitleColor:[UIColor colorWithWhite:0.7 alpha:0.9] forState:UIControlStateNormal];
    [but setTitleColor:redUIColor forState:UIControlStateSelected];
    [but setTitleColor:redUIColor forState:UIControlStateHighlighted];
    but.contentHorizontalAlignment = UIControlContentHorizontalAlignmentCenter ;//设置文字位置，现设为居中
    [but addTarget:self action:@selector(clickNum:) forControlEvents:(UIControlEventTouchUpInside)];
//    [but tapGesture:^(UIGestureRecognizer *ges) {
//        [self clickNum:but];
//    }];
    [self.numButtonArr addObject:but];
    return but;
}

- (void)clickNum:(UIButton *)send {
    [self changeDark];
    self.didSelected = send.tag;
    send.selected = true;
    [send border:redUIColor width:1 CornerRadius:5];
}
- (void)setBorder:(WHC_StackView *)stackView {
    stackView.layer.borderColor = RGBColor(235, 235, 235).CGColor;
    stackView.layer.borderWidth = 0.5;
}



- (NSMutableArray<UIButton *> *)numButtonArr{
    if (_numButtonArr == nil) {
        _numButtonArr = [NSMutableArray array];
    }
    return _numButtonArr;
}
@end
